/**
 * 接口和类
 */

interface Person {
  name: string;
  age: number;
  say(): void;
}

class Student implements Person {
  name: string;
  age: number;
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
  say() {
    console.log(`${this.name} ${this.age}`);
  }
}
let student = new Student("张三", 18);
student.say();


// 接口的可选属性
//可有age也可以没有
interface Person2 {
  name: string;
  age?: number;
  say(): void;
}
class Student2 implements Person2 {
  name: string;
  age?: number;
  constructor(name: string, age?: number) {
    this.name = name;
    this.age = age;
  }
  say() {
    console.log(`${this.name} ${this.age}`);
  }
}
let student2 = new Student2("张三", 18);
student2.say();


/**
 * 只读属性
 * readonly
 */
interface Person3 {
  readonly name: string;
  age?: number;
  say(): void;
}
class Student3 implements Person3 {
  name: string;
  age?: number;
  constructor(name: string, age?: number) {
    this.name = name;
    this.age = age;
  }
  say() {
    console.log(`${this.name} ${this.age}`);
  }
}
let student3 = new Student3("张三", 18);




/**
 * 可索引接口
 * 索引签名
 */
interface Person4 {
  readonly name: string;
  age?: number;
  hobby?: string[];
}
let obj1:Person4 = {
  name:"张三",
  age:18,
  hobby:["a","b"]
}